<?php
class App_Page_Service_Page
{
	/**
	 * @var App_Page_Model_Page
	 */
	protected $_modelPage;
	
	/**
	 * @var array
	 */
	protected $_forbiddenRootPageNames = array('admin', 'error');
	
	/**
	 * @return void
	 */
	public function __construct() 
	{
		$this->_modelPage = Main_Model::factory('Page_Model_Page');
	}
	
	/**
	 * @var App_Page_Model_Page
	 */
	public function getModelPage()
	{
		return $this->_modelPage;
	}
	
	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data)
	{
		$langId = Zend_Registry::getInstance()->langId;
		$data['name'] = trim($data['name']); 
		$data += array('lang_id' => $langId);
		return $this->_modelPage->insertItem($data);
	}
	
	/**
	 * @param array $data
	 * @param int $pageId
	 * @return bool
	 */
	public function updateItem($data, $pageId)
	{
		$langId = Zend_Registry::getInstance()->langId;
		$data['name'] = trim($data['name']);
		$data += array('lang_id' => $langId);
		return $this->_modelPage->updateItem($data, $pageId);
	}
	
	/**
	 * @param int $id
	 * @return array
	 */
	public function getItemById($id) 
	{
		$langId = Zend_Registry::getInstance()->langId;
		return $this->_modelPage->getItemById($id, $langId);
	}
	
	/**
	 * @return array
	 */
	public function getAll()
	{
		$list =  $this->_modelPage->getAll();
		return $list;		
	}
	
	/**
	 * @param int $id
	 * @return bool
	 */
	public function deleteItem($id) 
	{
		$node = $this->_modelPage->getNodeById($id);
		// --- delete root page is forbidden --- {{
		if ($node['level'] == 1) {
			return false;			
		}
		// --- delete root page is forbidden --- }}
		
		
		return $this->_modelPage->deleteItem($id);
	}
	
	/**
	 * @return array
	 */
	public function getAllIdAndName() 
	{
		return $this->_modelPage->getAllIdAndName();
	}
	
	/**	
     * @param string $value 
     * @param array $otherCond
     * @return bool
     */
    public function checkUnique ($value, $otherCond)
    {
    	$parentId = $otherCond['parent_id'];
    	$parentId = ($parentId != 0) ? $parentId : null;
    	$langId = Zend_Registry::getInstance()->langId; 
    	
        $list = $this->_modelPage->getItemByNameAndParentId($value, $parentId, $langId);
        return empty($list) ? true : false;
    }
    
    /**
     * @param int $id
     * @return array
     */
    public function getAscendantBranchIdAndName($id) 
    {
    	$list = $this->_modelPage->getAscendantBranchIdAndName($id);
    	return $list;
    }

    /**
     * @param string $name
     * @param int $parentId
     * @return bool
     */
    public function isNameForbidden($name, $parentId)
    {
    	$name = trim($name);
    	
    	$parentItem = $this->getItemById($parentId);
    	$parentLevel = $parentItem['level'];
    	
    	if ( ($parentLevel == 1) && (in_array($name, $this->_forbiddenRootPageNames)) ) {
			return true;    		
    	}
    	
		return false;    	
    }
    
	/**
	 * @param int $pageId
	 * @return array
	 */    
    public function getBlockStatisticsInPage($pageId)
    {
		return $this->_modelPage->getBlockStatisticsInPage($pageId);    	
    }
    
}
?>